package com.bdqn.systems.auth;

import com.bdqn.systems.service.impl.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author : CP
 * @version: 1.0
 * @program : xinguan-parent
 * @description :
 * @date : 2020-12-08 17:02
 **/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Autowired
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Autowired
    private MyLoginoutSuccessHandler myLoginoutSuccessHandler;

    @Autowired
    private MyAccessDeniedHandler myAccessDeniedHandler;

    @Autowired
    private MyAuthenticationEntryPoint myAuthenticationEntryPoint;

    @Autowired
    private MyUserDetailsService myUserDetailsService;

    /**
     * 身份验证管理器生成器
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService)
                .passwordEncoder(passwordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**", "/js/**", "/img/**");
    }

    /**
     * Http安全性
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 关闭csrf跨域
        http.csrf().disable();

        // 表单登录
        http.formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .successHandler(myAuthenticationSuccessHandler)
                .failureHandler(myAuthenticationFailureHandler);

        // 表单登出
        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/logout")
                //自定义logoutSuccessHandler
                .logoutSuccessHandler(myLoginoutSuccessHandler)
                //删除cookies
                .deleteCookies("JSESSIONID");

        // 权限拒绝，未登录访问处理
        http.exceptionHandling()
                // 权限拒绝
                .accessDeniedHandler(myAccessDeniedHandler)
                // 未登录访问
                .authenticationEntryPoint(myAuthenticationEntryPoint);

        // 会话管理
        http.sessionManagement()
                // 每次登录创建新的session
                .sessionFixation().migrateSession()
                // 单点登录
                .maximumSessions(1)
                // 不阻止登录
                .maxSessionsPreventsLogin(false)
                // 会话过期处理
                .expiredSessionStrategy(new MyExpiredSessionStrategy());

//        // 权限访问
//        http.authorizeRequests()
//                .anyRequest()
//                .authenticated();
    }
}
